package client.view;

import java.awt.BorderLayout;

import javax.swing.Action;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;

/**
 * A general interface component for displaying chat messages and entering new messages.
 */
public class ChatPane extends JPanel
{
	private JTextPane chatPane;
	private JTextField chatField;
	private String chatText = "";
	
	/**
	 * Construct a new ChatPane with the given Action.
	 * @param a an Action to fire when the user presses the Enter key in the text entry field
	 */
	public ChatPane(Action a)
	{
		super(new BorderLayout());
		chatField = new JTextField();
		chatPane = new JTextPane();
		
		chatField.setAction(a);
		chatPane.setEditable(false);
		chatPane.setContentType("text/html");
		
		add(new JScrollPane(chatPane));
		add(chatField, BorderLayout.SOUTH);
	}
	
	/**
	 * Appends a message to the list of previous messages
	 */
	public void appendMessage(String message)
	{
		chatText += "<br>" + message;
		chatPane.setText("<html>" + chatText + "</html>");
	}
	
	/**
	 * Returns the text that is currently displayed in the text entry field
	 */
	public String getMessage()
	{
		return chatField.getText();
	}
	
	/**
	 * Clears the contents of the text entry field and the previous messages.
	 */
	public void clear()
	{
		chatText = "";
		chatPane.setText("<html>" + chatText + "</html>");
		clearMessage();
	}
	
	
	public void clearMessage()
	{
		chatField.setText("");
	}
}
